/*
 * @(#)AbstractNamedObject.java	1.0 2000/12/14
 *
 * Copyright 2000 Thomas Barnekow. All Rights Reserved.
 *
 * This software is the proprietary information of Thomas Barnekow.
 * Use is subject to license terms.
 */

package tb.util;

/**
 * This class can can serve as a base class for named objects.
 *
 * @author Thomas Barnekow
 * @version 1.0
 */
public abstract class AbstractNamedObject implements NamedObject {

	private QualifiedName m_qn;

	public AbstractNamedObject(QualifiedName name) {
		if (name == null) {
			throw new IllegalArgumentException("Name must not be null");
		}
		m_qn = name;
	}

	public AbstractNamedObject(String name) {
		this(new QualifiedName(name));
	}

	public QualifiedName getQualifiedName() {
		return m_qn;
	}

	/*
	 * Object operations
	 */

	public boolean equals(Object o) {
    return compareTo(o) == 0;
	}

	public int hashCode() {
		return getQualifiedName().hashCode();
	}

	public String toString() {
		StringBuffer buffer = new StringBuffer(64);
		buffer.append('[');
		buffer.append(getClass().getName()).append(':').append(getQualifiedName());
		buffer.append(']');
		return buffer.toString();
	}

	/*
	 * Comparable operations
	 */

	public int compareTo(Object o) {
		if (o == null)
			throw new NullPointerException();
    else if (o instanceof NamedObject)
      return getQualifiedName().compareTo(((NamedObject) o).getQualifiedName());
    else if (o instanceof QualifiedName)
      return getQualifiedName().compareTo(o);
    else if (o instanceof String)
      return getQualifiedName().toString().compareTo((String) o);
    else
  		throw new ClassCastException();
	}
}
